﻿Imports System.Linq
Imports MNL.Store

Partial Class CheckOut
    Inherits System.Web.UI.Page
    Private dc As MNLDataContext

    Protected Function FormatPrice(ByVal price As Object) As String
        Return Convert.ToDecimal(price).ToString("N2") + " " + "£"
    End Function


    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender

        BindOrderedPartsGrd()

    End Sub

    Private Sub BindOrderedPartsGrd()

        Dim OrderedParts = CurrentUserShoppingCart.GetItems()
        If OrderedParts IsNot Nothing AndAlso OrderedParts.Count > 0 Then

            lblSubtotal.Text = FormatPrice(Profile.ShoppingCart.Total)

            If Session("shippingCost") IsNot Nothing Then

                lblShippingCost.Text = Session("shippingCost").ToString
                lblTotal.Text = FormatPrice(Profile.ShoppingCart.Total + Decimal.Parse(Session("shippingCost")))

            End If

        Else
            panTotals.Visible = False
        End If

        gvwOrderedParts.DataSource = OrderedParts
        gvwOrderedParts.DataBind()

    End Sub


    Protected Sub btnOrder_Click(ByVal sender As Object, ByVal e As System.EventArgs)

        'make sure the customer is still logged in
        If Not User.Identity.IsAuthenticated Then
            Response.Redirect("~/login.aspx")
        End If

        dc = New MNLDataContext
        Dim customerName As String = User.Identity.Name
        Dim shoppingCart = Profile.ShoppingCart
        Dim Subtotal = shoppingCart.Total
        Dim shippingCost = Decimal.Parse(lblShippingCost.Text)
        Dim orderedDate = DateTime.Now

        Try
            'add order
            Dim order As New Order
            order.DateOrdered = orderedDate
            order.StatusID = 3 'status is 'processing' as defined in order statuses table
            order.OrderedBy = customerName
            order.SubTotal = Subtotal
            order.ShippingCost = shippingCost
            order.Total = Subtotal + shippingCost

            dc.Orders.InsertOnSubmit(order)
            dc.SubmitChanges()

            Dim orderIDJustAdded = order.OrderID ' save the order id just inserted into the order table

            For Each PartItem As ShoppingCartItem In shoppingCart.Parts
                'add order details

                Dim orderItems As New OrderItem

                orderItems.OrderID = orderIDJustAdded
                orderItems.PartID = PartItem.PartID
                orderItems.Quantity = PartItem.Quantity
                orderItems.UnitPrice = PartItem.UnitPrice

                dc.OrderItems.InsertOnSubmit(orderItems)
            Next

            dc.SubmitChanges()

            lblOrderFeedBack.Text = "Thank you for order. After we have processed your order, we will arrange for delivery to your address"
            lblOrderFeedBack.ForeColor = Drawing.Color.Blue
        Catch ex As Exception

            lblOrderFeedBack.Text = "An error occoured in placing your order. The details of this error has been emailed to the site manager. We regret any inconveniences caused by this"
            lblOrderFeedBack.ForeColor = Drawing.Color.Red

        Finally

        End Try

        'clear shoppin cart
        Profile.ShoppingCart.Clear()

        lblCartHeader.Text = "Latest News"
    End Sub
End Class
